package com.UnionPayCenter.distribution.service.impl;

import com.UnionPayCenter.constant.ConstantInterface;
import com.UnionPayCenter.distribution.constants.DistributionConstants;
import com.UnionPayCenter.distribution.constants.TemplateDefaultConstants;
import com.UnionPayCenter.distribution.dto.*;
import com.UnionPayCenter.distribution.entity.*;
import com.UnionPayCenter.distribution.manager.*;
import com.UnionPayCenter.distribution.service.DistributionQueryService;
import com.UnionPayCenter.distribution.vo.*;
import com.UnionPayCenter.entity.ConfEntity;
import com.UnionPayCenter.entity.CouponEntity;
import com.UnionPayCenter.entity.UserEntity;
import com.UnionPayCenter.entity.UserLevelEntity;
import com.UnionPayCenter.mapper.ConfMapper;
import com.UnionPayCenter.service.ICouponService;
import com.UnionPayCenter.service.IUserLevelService;
import com.UnionPayCenter.service.IUserService;
import com.UnionPayCenter.util.CSVUtil;
import com.UnionPayCenter.util.DateFormatUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Noah
 * @since 1.0.0, 2019/07/07
 */
@Service
public class DistributionQueryServiceImpl implements DistributionQueryService {

    @Autowired
    private DistributionTemplateManager templateManager;

    @Autowired
    private SharePropManager sharePropManager;

    @Autowired
    private UserDistributionRefManager userDistributionRefManager;

    @Autowired
    private UserDistributionManager userDistributionManager;

    @Autowired
    private DistributionSnManager distributionSnManager;

    @Autowired
    private IUserService userService;

    @Autowired
    private ConfMapper confMapper;

    @Autowired
    private DistributionTemplateCardManager templateCardManager;

    @Autowired
    private ICouponService couponService;

    @Autowired
    private IUserLevelService userLevelService;


    /**
     * 获取商品的分润比例信息
     *
     * @param baseDTO 查询基本信息
     * @return 分润比例
     */
    @Override
    public DistributionTemplateVO queryShareProp(DistributionBaseDTO baseDTO) {
        DistributionTemplateVO distributionTemplateVO = new DistributionTemplateVO();
        distributionTemplateVO.setFlag(DistributionConstants.FALSE_STRING);
        distributionTemplateVO.setEffectNextDay(DistributionConstants.FALSE_STRING);
        DistributionTemplateDO templateDO = templateManager.queryTemplate(baseDTO);
        if (Objects.isNull(templateDO)) {
            return distributionTemplateVO;
        }

        List<SharePropDO> sharePropDOList = sharePropManager.listByTemplateId(String.valueOf(templateDO.getIdgen()));
        if (CollectionUtils.isEmpty(sharePropDOList)) {
            return distributionTemplateVO;
        }
        List<SharePropVO> sharePropVOList = sharePropDOList.stream().map(e -> {
            SharePropVO sharePropVO = new SharePropVO();
            sharePropVO.setPropIdgen(e.getPropIdgen());
            sharePropVO.setLevel(e.getLvl());
            sharePropVO.setProp(DistributionConstants.HUNDRED.multiply(e.getProp()));
            return sharePropVO;
        }).collect(Collectors.toList());
        distributionTemplateVO.setFlag(templateDO.getFlag());
        distributionTemplateVO.setSharePropVOList(sharePropVOList);
        distributionTemplateVO.setEffectNextDay(String.valueOf(templateDO.getEffectNextDayFlag()));
       String cardLevel = templateCardManager.getMemberShipCard(baseDTO.getShopId(), templateDO.getIdgen());
        distributionTemplateVO.setCounterpartyLevelId(null == cardLevel ? null : Integer.valueOf(cardLevel));
        distributionTemplateVO.setUserIncentiveCardList(converterCardVO(templateCardManager.listUserTemplateCard(baseDTO.getShopId(), templateDO.getIdgen())));
        distributionTemplateVO.setCounterpartyUserIncentiveCardList(converterCardVO(templateCardManager.listCounterpartyUserTemplateCard(baseDTO.getShopId(), templateDO.getIdgen())));
        return distributionTemplateVO;
    }

    private List<DistributionTemplateCardVO> converterCardVO(List<DistributionTemplateCardDO> cardDOList) {
        if (CollectionUtils.isEmpty(cardDOList)) {
            return Collections.emptyList();
        }
        return cardDOList.stream().map(e -> {
            DistributionTemplateCardVO cardVO = new DistributionTemplateCardVO();
            cardVO.setCardId(Integer.valueOf(e.getCardId()));
            cardVO.setCardNumber(e.getCardNumber());
            return cardVO;
        }).collect(Collectors.toList());
    }

    /**
     * 根据对手ID获取用户信息
     *
     * @param bId                商铺ID
     * @param shopId             店铺ID
     * @param counterpartyUserId 对手ID
     * @return
     */
    @Override
    public UserDistributionDO getUserDistributionDoByCounterpartyUser(Long bId, Long shopId, Long counterpartyUserId) {
        UserDistributionRefDO refDO = userDistributionRefManager.selectByCounterpartyUser(bId, shopId, counterpartyUserId);
        if (Objects.isNull(refDO)) {
            return null;
        }
        return userDistributionManager.getPayUserDistributionByUserId(bId, shopId, refDO.getUserId());
    }

    /**
     * 根据用户查询用户账户信息
     *
     * @param userBaseDTO 用户信息
     * @return 用户分销信息
     */
    @Override
    public DistributionUserAccountVO getDistributionAccount(DistributionUserBaseDTO userBaseDTO) {
        DistributionTemplateDO distributionTemplateDO = templateManager.queryTemplate(userBaseDTO);
        String flag = Objects.isNull(distributionTemplateDO) ? DistributionConstants.FALSE_STRING : DistributionConstants.TURE_STRING;

        int earningsAmount = distributionSnManager.sumShareAmountByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),null);
        int firstInviteNum = userDistributionRefManager.countRecordFilterBlankMobileByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),1);
        int secondInviteNum = userDistributionRefManager.countRecordFilterBlankMobileByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),2);

        UserDistributionRefDO refDO = userDistributionRefManager.selectByCounterpartyUser(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId());
        if (Objects.isNull(refDO)) {
            refDO = new UserDistributionRefDO();
        }

        int firstOrders = distributionSnManager.countRecordByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),1);
        int secondOrders = distributionSnManager.countRecordByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),2);

        return DistributionUserAccountVO.builder()
                .earningsAmount(earningsAmount)
                .inviterNum(firstInviteNum+secondInviteNum)
                .orders(firstOrders+secondOrders)
                .userId(refDO.getUserId())
                .userName(refDO.getUserName())
                .flag(flag)
                .build();
    }

    /**
     * 根据用户查询用户账户收益流水列表 （按照时间降序）
     *
     * @param userBaseDTO 用户信息
     * @return 用户账户收益流水列表
     */
    @Override
    public PageInfo<DistributionUserEarningsVO> pageDistributionEarningsByUserId(DistributionUserBasePageDTO userBaseDTO) {
        Page<DistributionUserEarningsVO> page = new Page<>();
        Page<DistributionSnDO> list = distributionSnManager.listDistributionSn(userBaseDTO.getBId(), userBaseDTO.getShopId(),
                userBaseDTO.getUserId(),userBaseDTO.getUserLevel(), userBaseDTO);
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page);
        }
        BeanUtils.copyProperties(list, page);
        for (DistributionSnDO e : list) {
            DistributionUserEarningsVO earnings = new DistributionUserEarningsVO();
            earnings.setId(e.getDisSn());
            earnings.setCounterpartyUserId(e.getCounterpartyUserId());
            earnings.setCounterpartyUserMobile(fuzzyMobile(getUserMobile(e.getCounterpartyUserId().intValue())));
            earnings.setCreateTime(DateFormatUtil.dateToString(e.getCreateTime()));
            earnings.setEarnings(e.getShareAmount());
            earnings.setEarnings(e.getShareAmount());
            page.add(earnings);
        }
        return new PageInfo<>(page);
    }


    private String getUserMobile(Integer userId) {
        if (Objects.isNull(userId)) {
            return StringUtils.EMPTY;
        }
        UserEntity userEntity = userService.getUserById(userId);
        if (Objects.isNull(userEntity)) {
            return StringUtils.EMPTY;
        }
        return userEntity.getMobile();

    }

    /**
     * 根据用户查询用户邀请列表（按照时间降序）
     *
     * @param userBaseDTO 用户信息
     * @return 邀请人信息列表
     */
    @Override
    public PageInfo<DistributionUserInviterVO> pageDistributionInviterByUserId(DistributionUserBasePageDTO userBaseDTO) {
        Page<DistributionUserInviterVO> page = new Page<>();

        Page<UserDistributionRefDO> list =  null;
        if(userBaseDTO.getUserLevel().equals(2)){
            list =userDistributionRefManager.pageSecondRecordFilterBlankMobileByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),userBaseDTO);
        }else if(userBaseDTO.getUserLevel().equals(1)){
            list =userDistributionRefManager.pageFirstRecordFilterBlankMobileByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),userBaseDTO);
        }else {
            list =userDistributionRefManager.pageAllRecordFilterBlankMobileByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(),userBaseDTO);
        }
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page);
        }
        BeanUtils.copyProperties(list, page);
        for (UserDistributionRefDO e : list) {
            UserEntity userEntity = userService.getUserById(e.getCounterpartyUserId().intValue());
            if(Objects.isNull(userEntity)){
                userEntity = new UserEntity();
            }
            DistributionUserInviterVO inviterVO = new DistributionUserInviterVO();
            inviterVO.setId(e.getIdgen().longValue());
            inviterVO.setCounterpartyUserId(e.getCounterpartyUserId());
            inviterVO.setCounterpartyUserMobile(fuzzyMobile(getUserMobile(e.getCounterpartyUserId().intValue())));
            inviterVO.setCreateTime(DateFormatUtil.dateToString(userEntity.getCreateDate()));
            int earningsAmount = distributionSnManager.sumShareAmountByUserId(userBaseDTO.getBId(), userBaseDTO.getShopId(), e.getCounterpartyUserId(),null);
            inviterVO.setEarnings(earningsAmount);
            page.add(inviterVO);
        }
        return new PageInfo<>(page);
    }

    /**
     * 根据用户查询用户账户订单列表 （按照时间降序）
     *
     * @param userBaseDTO 用户信息
     * @return 邀请人信息列表
     */
    @Override
    public PageInfo<DistributionUserOrderVO> pageDistributionOrderByUserId(DistributionUserBasePageDTO userBaseDTO) {
        Page<DistributionUserOrderVO> page = new Page<>();
        Page<DistributionSnDO> list = distributionSnManager.pageDistributionSn(userBaseDTO.getBId(), userBaseDTO.getShopId(), userBaseDTO.getUserId(), userBaseDTO.getUserLevel(), userBaseDTO.getPage(), userBaseDTO.getPageSize());
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page);
        }
        BeanUtils.copyProperties(list, page);
        for (DistributionSnDO e : list) {
            DistributionUserOrderVO orderVO = new DistributionUserOrderVO();
            orderVO.setShareSn(e.getShareSn());
            orderVO.setUserId(e.getUserId());
            orderVO.setUserMobile(fuzzyMobile(e.getUserMobile()));
            orderVO.setUserName(e.getUserName());
            orderVO.setActualAmount(e.getActualAmount());
            orderVO.setCounterpartyUserId(e.getCounterpartyUserId());
            orderVO.setCounterpartyUserMobile(fuzzyMobile(getUserMobile(e.getCounterpartyUserId().intValue())));
            orderVO.setCounterpartyUserName(e.getCounterpartyUserName());
            orderVO.setDisSn(e.getDisSn());
            orderVO.setShareAmount(e.getShareAmount());
            orderVO.setCreateTime(DateFormatUtil.dateToString(e.getCreateTime()));
            page.add(orderVO);
        }
        return new PageInfo<>(page);

    }

    /**
     * 根据用户查询用户账户订单列表 （按照时间降序）
     *
     * @param userMobileDTO 用户信息
     * @return 邀请人信息列表
     */
    @Override
    public List<DistributionUserOrderVO> listDistributionOrderByUserMobile(DistributionUserMobileDTO userMobileDTO) {
        UserEntity userEntity = userService.getUserByMobile(userMobileDTO.getMobile(), userMobileDTO.getShopId().intValue());
        Long userId = null;
        if (Objects.nonNull(userEntity)) {
            userId = userEntity.getUserId().longValue();
        }
        List<DistributionSnDO> list = distributionSnManager.listDistributionSn(userMobileDTO.getBId(), userMobileDTO.getShopId(), userId);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(e -> {
            DistributionUserOrderVO orderVO = new DistributionUserOrderVO();
            orderVO.setShareSn(e.getShareSn());
            orderVO.setUserId(e.getUserId());
            orderVO.setUserMobile(e.getUserMobile());
            orderVO.setUserName(e.getUserName());
            orderVO.setActualAmount(e.getActualAmount());
            orderVO.setCounterpartyUserId(e.getCounterpartyUserId());
            orderVO.setCounterpartyUserMobile(getUserMobile(e.getCounterpartyUserId().intValue()));
            orderVO.setCounterpartyUserName(e.getCounterpartyUserName());
            orderVO.setDisSn(e.getDisSn());
            orderVO.setShareAmount(e.getShareAmount());
            orderVO.setCreateTime(DateFormatUtil.dateToString(e.getCreateTime()));
            return orderVO;
        }).collect(Collectors.toList());
    }


    /**
     * 根据用户查询用户账户订单列表 （按照时间降序）
     *
     * @param userBaseDTO 用户信息
     * @return 邀请人信息列表
     */
    @Override
    public PageInfo<DistributionUserOrderVO> pageDistributionOrderByUserMobile(DistributionUserMobilePageDTO userBaseDTO) {
        Page<DistributionUserOrderVO> page = new Page<>();
        UserEntity userEntity = userService.getUserByMobile(userBaseDTO.getMobile(), userBaseDTO.getShopId().intValue());
        Long userId = null;
        if (Objects.nonNull(userEntity)) {
            userId = userEntity.getUserId().longValue();
        }

        Page<DistributionSnDO> list = distributionSnManager.pageDistributionSn(userBaseDTO.getBId(), userBaseDTO.getShopId(),
                userId, null, userBaseDTO.getPage(), userBaseDTO.getPageSize());
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page);
        }
        BeanUtils.copyProperties(list, page);
        for (DistributionSnDO snDO : list) {
            DistributionUserOrderVO orderVO = new DistributionUserOrderVO();
            orderVO.setShareSn(snDO.getShareSn());
            orderVO.setUserId(snDO.getUserId());
            orderVO.setUserMobile(getUserMobile(snDO.getUserId().intValue()));
            orderVO.setUserName(snDO.getUserName());
            orderVO.setActualAmount(snDO.getActualAmount());
            orderVO.setCounterpartyUserId(snDO.getCounterpartyUserId());
            orderVO.setCounterpartyUserMobile(getUserMobile(snDO.getCounterpartyUserId().intValue()));
            orderVO.setCounterpartyUserName(snDO.getCounterpartyUserName());
            orderVO.setDisSn(snDO.getDisSn());
            orderVO.setShareAmount(snDO.getShareAmount());
            orderVO.setCreateTime(DateFormatUtil.dateToString(snDO.getCreateTime()));
            page.add(orderVO);
        }
        return new PageInfo<>(page);
    }

    /**
     * 手机号模糊
     *
     * @param mobile
     * @return
     */
    private String fuzzyMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return null;
        }
        String phoneNumber = "";
        if (StringUtils.isNotEmpty(mobile) && mobile.length() == 11) {
            phoneNumber = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
        return phoneNumber;
    }

    @Override
    public String exportShopOrder(List<DistributionUserOrderVO> orderVOList) throws IOException {
        String[] propertyName = new String[6];
        propertyName[0] = "分销员手机号";
        propertyName[1] = "订单流水号";
        propertyName[2] = "订单金额";
        propertyName[3] = "返利金额";
        propertyName[4] = "订单时间";
        propertyName[5] = "客户手机号";

        List<List<String>> exportData = new ArrayList<>();
        List<String> row;
        if (orderVOList != null) {
            for (DistributionUserOrderVO orderVO : orderVOList) {
                row = new ArrayList<>();
                row.add(orderVO.getUserMobile());
                row.add(orderVO.getShareSn());
                row.add(BigDecimal.valueOf(orderVO.getActualAmount() / 100d).toPlainString());
                row.add(BigDecimal.valueOf(orderVO.getShareAmount() / 100d).toPlainString());
                row.add(orderVO.getCreateTime());
                row.add(getUserMobile(orderVO.getCounterpartyUserId().intValue()));
                exportData.add(row);
            }
        }

        String name = "/DistributionUserOrder-" + DateFormatUtil.getYYYY_MM_DD() + ".xls";
        CSVUtil.poiExcel(name, "商铺分销业绩导出", propertyName, exportData);

        ConfEntity confEntity = confMapper.selectByKey(ConstantInterface.DOMAIN_URL);
        return confEntity.getConfValue() + name;
    }

    /**
     * 根据手机号获取分销员列表信息
     *
     * @param mobilePageDTO 手机号查询条件
     * @return
     */
    @Override
    public PageInfo<DistributionUserVO> pageDistributionUserByMobile(DistributionUserByMobilePageDTO mobilePageDTO) {
        Page<DistributionUserVO> page = new Page<>();
        if (Objects.isNull(mobilePageDTO)) {
            return new PageInfo<>(page);
        }
        Long userId = null;
        Long counterpartyUserId = null;
        if (StringUtils.isNotBlank(mobilePageDTO.getUserMobile())) {
            UserEntity userEntity = userService.getUserByMobile(mobilePageDTO.getUserMobile(), mobilePageDTO.getShopId().intValue());
            userId = Objects.isNull(userEntity) ? null : userEntity.getUserId().longValue();
        }
        if (StringUtils.isNotBlank(mobilePageDTO.getCounterpartyUserMobile())) {
            UserEntity userEntity = userService.getUserByMobile(mobilePageDTO.getCounterpartyUserMobile(), mobilePageDTO.getShopId().intValue());
            counterpartyUserId = Objects.isNull(userEntity) ? null : userEntity.getUserId().longValue();
        }

        UserDistributionRefDO refQuery = new UserDistributionRefDO();
        refQuery.setUserId(userId);
        refQuery.setCounterpartyUserId(counterpartyUserId);
        refQuery.setShopId(mobilePageDTO.getShopId());
        refQuery.setBId(mobilePageDTO.getBId());

        Page<UserDistributionRefDO> rsPage = userDistributionRefManager.pageDistributionSn(refQuery, mobilePageDTO.getPage(), mobilePageDTO.getPageSize());
        BeanUtils.copyProperties(rsPage, page);
        for (UserDistributionRefDO refDO : rsPage) {
            DistributionUserVO userVO = new DistributionUserVO();
            userVO.setCounterpartyUserId(refDO.getCounterpartyUserId());
            userVO.setCounterpartyUserMobile(getUserMobile(refDO.getCounterpartyUserId().intValue()));
            userVO.setCounterpartyUserName(refDO.getCounterpartyUserName());
            userVO.setUserId(refDO.getUserId());
            userVO.setUserName(refDO.getUserName());
            userVO.setUserMobile(getUserMobile(refDO.getUserId().intValue()));
            userVO.setInviterTotal(userDistributionRefManager.
                    countAllRecordByUserId(refDO.getBId(), refDO.getShopId(), refDO.getCounterpartyUserId(), TemplateDefaultConstants.SHARE_LVL));
            userVO.setEarningsAmount(distributionSnManager.sumShareAmountByUserId(refDO.getBId(), refDO.getShopId(), refDO.getCounterpartyUserId(),null));
            page.add(userVO);
        }
        return new PageInfo<>(page);
    }

    /**
     * 获取用户所有的关系
     *
     * @param mobileDTO 手机号
     * @return
     */
    @Override
    public DistributionUserRefVO listAllDistributionUserRef(DistributionUserMobileDTO mobileDTO) {
        if (StringUtils.isBlank(mobileDTO.getMobile())) {
            return null;
        }
        DistributionUserRefVO refVO = new DistributionUserRefVO();
        UserEntity userEntity = userService.getUserByMobile(mobileDTO.getMobile(), mobileDTO.getShopId().intValue());
        if (Objects.isNull(userEntity)) {
            return null;
        }
        Long userId = userEntity.getUserId().longValue();
        refVO.setSubUser(listAllDistributionUserRef(mobileDTO.getBId(), mobileDTO.getShopId(), userId, 2, 1));
        refVO.setUserMobile(mobileDTO.getMobile());
        return refVO;
    }


    /**
     * 递归获取下级用户信息
     *
     * @param bId        商铺ID
     * @param shopId     店铺ID
     * @param userId     用户ID
     * @param totalLevel 总级
     * @param level      当前级数
     * @return
     */
    private List<DistributionUserRefVO> listAllDistributionUserRef(Long bId, Long shopId, Long userId, Integer totalLevel, Integer level) {
        List<UserDistributionRefDO> refDOList = userDistributionRefManager.listByUserId(bId, shopId, userId);
        if (CollectionUtils.isEmpty(refDOList) || totalLevel.compareTo(level) < 0) {
            return Collections.emptyList();
        }
        final Integer lvl = level + 1;
        return refDOList.stream().map(e -> {
            DistributionUserRefVO refVO = new DistributionUserRefVO();
            refVO.setUserId(e.getUserId());
            refVO.setUserMobile(getUserMobile(e.getCounterpartyUserId().intValue()));
            refVO.setUserName(e.getCounterpartyUserName());
            refVO.setSubUser(listAllDistributionUserRef(bId, shopId, e.getCounterpartyUserId(), totalLevel, lvl));
            return refVO;
        }).collect(Collectors.toList());
    }

    /**
     * 获取奖励信息
     *
     * @param distributionBaseDTO
     * @return
     */
    @Override
    public DistributionIncentiveVO getIncentive(DistributionBaseDTO distributionBaseDTO) {
        DistributionIncentiveVO incentiveVO = new DistributionIncentiveVO();
        DistributionTemplateDO templateDO = templateManager.queryTemplate(distributionBaseDTO);
        List<CouponEntity> couponEntities = Lists.newArrayList();
        if (Objects.isNull(templateDO)) {
            incentiveVO.setCouponEntityList(couponEntities);
            return incentiveVO;
        }
        String cardLevelId = templateCardManager.getMemberShipCard(templateDO.getShopId(), templateDO.getIdgen());
        List<DistributionTemplateCardDO> cardDOList = templateCardManager.listCounterpartyUserTemplateCard(templateDO.getShopId(), templateDO.getIdgen());
        if (CollectionUtils.isNotEmpty(cardDOList)) {
            for (DistributionTemplateCardDO cardDO : cardDOList) {
                CouponEntity couponEntity = couponService.getCouponMapperByTypeId(Long.valueOf(cardDO.getCardId()));
                couponEntities.add(couponEntity);
            }
        }
        UserLevelEntity userLevelEntity = userLevelService.selectByLevelId(templateDO.getShopId().intValue(), Integer.valueOf(cardLevelId));
        incentiveVO.setUserLevelEntity(userLevelEntity);
        incentiveVO.setCouponEntityList(couponEntities);
        return incentiveVO;
    }
}
